package com.taiyuan.tongcheng.db;

import java.util.List;

import org.kymjs.kjframe.KJDB;

import android.content.Context;

import com.taiyuan.tongcheng.bean.SupportCityInfo;
import com.taiyuan.tongcheng.bean.UserInfo;
/**
 * 
* @Description 数据库操作
* @author Administrator  
* @date 2015年9月20日 上午1:02:41 
* @update (date)
* @version V1.0
 */
public class TcDbHelper {
    private static UserInfo user = null;

    public static void saveUser(Context cxt, UserInfo u) {
        KJDB kjdb = KJDB.create(cxt);
        kjdb.deleteByWhere(UserInfo.class, "");
        user = u;
        kjdb.save(u);
    }

    public static void deleteUser(Context cxt){
    	  KJDB kjdb = KJDB.create(cxt);
          kjdb.deleteByWhere(UserInfo.class, "");
    }
    
    public static UserInfo getUser(Context cxt) {
        KJDB kjdb = KJDB.create(cxt);
        List<UserInfo> datas = kjdb.findAll(UserInfo.class);

        if (datas != null && datas.size() > 0) {
            user = datas.get(0);
        } else {
//            user = new UserInfo();
//            kjdb.save(user);
        	user=null;
        }
        return user;
    }
    
    
    public static void updataUser(Context cxt ,UserInfo  info){
    	        KJDB kjdb = KJDB.create(cxt,true);
    	        List<UserInfo> notifiInfoList= kjdb.findAllByWhere(UserInfo.class, "uid="+info.getUid());
    	        if(notifiInfoList!=null&&notifiInfoList.size()>0){
    	        	kjdb.update(info,"uid="+info.getUid());
    	        }else{
    	        	kjdb.save(info);
    	        }
    	     
    }
    

    public static void saveCityInfo(Context cxt, SupportCityInfo u) {
        KJDB kjdb = KJDB.create(cxt);
        kjdb.save(u);
    }
    
    
    public static void deleteCityInfo(Context cxt){
        KJDB kjdb = KJDB.create(cxt);
        kjdb.deleteByWhere(SupportCityInfo.class, "");
    }
    
    
    public static List<SupportCityInfo> getCityInfo(Context cxt) {
        KJDB kjdb = KJDB.create(cxt);
        List<SupportCityInfo> datas = kjdb.findAll(SupportCityInfo.class);

        if (datas != null && datas.size() > 0) {
        	return datas;
        } else {
        	return null;
        }
    }
    /**
     * 
    * @Description: 根据名称获取支持
    * @param cxt
    * @return
    * @return List<SupportCityInfo>
    * @throws
    * @autour Administrator
    * @date 2015年9月14日 下午10:30:24 
    * @update (date)
     */
	public static SupportCityInfo getCityInfoByCity(Context cxt, String name) {
		KJDB kjdb = KJDB.create(cxt);
		List<SupportCityInfo> datas = kjdb.findAllByWhere(
				SupportCityInfo.class, "name = "+"'"+name+"'");
//		List<SupportCityInfo> datas = kjdb.findAllByWhere(
//				SupportCityInfo.class, "name = " + name);
		if (datas != null && datas.size() > 0) {
			return datas.get(0);
		} else {
			return null;
		}
	}
}
